package com.se2.antiqueexhibition.Mapper;

import com.se2.antiqueexhibition.pojo.Admin;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface AdminMapper {

    @Insert("INSERT INTO t_admin (username, password, real_name, phone, department, role, avatar_url) " +
            "VALUES (#{username}, #{password}, #{realName}, #{phone}, #{department}, #{role}, #{avatarUrl})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Admin admin);

    @Select("SELECT * FROM t_admin WHERE username = #{username} AND status = 1")
    Admin selectByUsername(String username);

    @Select("SELECT * FROM t_admin WHERE id = #{id}")
    Admin selectById(Integer id);

    @Select("SELECT * FROM t_admin WHERE status = 1 ORDER BY id DESC")
    List<Admin> selectAll();

    @Select("SELECT * FROM t_admin ORDER BY id DESC")
    List<Admin> selectAllIncludingInactive();

    @Update("UPDATE t_admin SET real_name = #{realName}, phone = #{phone}, department = #{department}, " +
            "role = #{role}, avatar_url = #{avatarUrl}, update_time = NOW() WHERE id = #{id}")
    int update(Admin admin);

    @Update("UPDATE t_admin SET password = #{password}, update_time = NOW() WHERE id = #{id}")
    int updatePassword(@Param("id") Integer id, @Param("password") String password);

    @Update("UPDATE t_admin SET status = #{status}, update_time = NOW() WHERE id = #{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") Integer status);

    @Delete("DELETE FROM t_admin WHERE id = #{id}")
    int deleteById(Integer id);

    @Select("SELECT COUNT(*) FROM t_admin WHERE username = #{username}")
    int countByUsername(String username);

    @Select("SELECT COUNT(*) FROM t_admin WHERE phone = #{phone}")
    int countByPhone(String phone);
}
